﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace StoreTools.Tools
{
    public static class ParseFile
    {
        /// <summary>
        /// 读取文件内容，返回行列表
        /// </summary>
        /// <param name="path">文件路径</param>
        public static List<string> GetFileLines(string path)
        {
            if (string.IsNullOrEmpty(path)) return new List<string>();
            if (!File.Exists(path)) return new List<string>();
            return File.ReadAllLines(path).ToList();
        }

        /// <summary>
        /// 将列表内容写入文件中，覆盖写入
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="valuelist">需要写入的内容列表</param>
        public static void WriteListToFile(string path, List<string> valuelist) 
        {
            StringBuilder contents = new StringBuilder();
            foreach (string line in valuelist)
            {
                contents.AppendLine(line);
            }
            File.WriteAllText(path, contents.ToString());
        }

        /// <summary>
        /// 将列表内容写入文件中，追加写入
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="valuelist">需要写入的内容列表</param>
        public static void AppenListToFile(string path, List<string> valuelist)
        {
            StringBuilder contents = new StringBuilder();
            foreach (string line in valuelist)
            {
                contents.AppendLine(line);
            }
            File.AppendAllText(path, contents.ToString());
        }

        /// <summary>
        /// 判断文件目录是否存在，不存在则创建该目录
        /// </summary>
        /// <param name="dir">文件目录</param>
        public static void CheckDir(string dir)
        {
            if(!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
        }

        /// <summary>
        /// 判断文件目录是否存在，不存在则创建该目录，如果存在，支持删除后重新创建
        /// </summary>
        /// <param name="dir">文件目录</param>
        /// <param name="isClean">是否删除已存在目录</param>
        public static void CheckDir(string dir,bool isClean)
        {
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            else
            {
                if (isClean)
                {
                    Directory.Delete(dir, true);
                    Directory.CreateDirectory(dir);
                }
            }
        }
    }
}
